package test;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;


import java.io.IOException;
import java.text.DecimalFormat;
public class Main {
    public static void main(String[] args) {
        // 创建httpclient实例
        CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build();
        // 创建HttpGet实例，设置请求参数
        HttpGet httpGet = new HttpGet("http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=5000&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=1000&week=&systemType=PC");
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).build();
        httpGet.setConfig(requestConfig);
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36");
        httpGet.addHeader("content-type","application/json;charset=UTF-8");
        httpGet.addHeader("Accept","*/*");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Cookie","HMF_CI=1fd1a5272428f0ed7cf3234fd81be30bf69146b3f289fe9d1871945e9d2f71647c01a7739bb4a46954bd78cd5f874214280d30707e9d0f1bce714b2271d2c6e2fe; 21_vq=7");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Connection","keep-alive");

        try {
            // 执行http get请求
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            // 获取请求结果
            HttpEntity httpEntity = httpResponse.getEntity();
            String jsonString = EntityUtils.toString(httpEntity);
            // 解析JSON数据
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            JSONArray resultArray = jsonObject.getJSONArray("result");
            System.out.println("resultArray::"+resultArray.size());
            // 初始化数组，用于存放红球号码出现的次数
            int[] redBallCount = new int[33];
            // 初始化数组，用于存放蓝球号码出现的次数
            int[] blueBallCount = new int[16];
            for (int i = 0; i < resultArray.size(); i++) {
                JSONObject resultObject = resultArray.getJSONObject(i);
                // 获取开奖号码
                String[] numArray = resultObject.getString("red").split(",");
                String blueNum = resultObject.getString("blue");
                // 更新红球号码出现的次数
                for (String num : numArray) {
                    redBallCount[Integer.parseInt(num) - 1]++;
                }
                // 更新蓝球号码出现的次数
                blueBallCount[Integer.parseInt(blueNum) - 1]++;
            }
            // 计算红球号码出现的概率
            double[] redBallProbability = new double[33];
            for (int i = 0; i < redBallCount.length; i++) {
                redBallProbability[i] = (double)redBallCount[i] / (resultArray.size() * 6);
            }
            // 计算蓝球号码出现的概率
            double[] blueBallProbability = new double[16];
            for (int i = 0; i < blueBallCount.length; i++) {
                blueBallProbability[i] = (double)blueBallCount[i] / resultArray.size();
            }
            // 选择概率最高的一组红球号码和蓝球号码
            double maxProbability = 0;
            int[] chosenRedBall = new int[6];
            int chosenBlueBall = 0;
            for (int i = 0; i < redBallProbability.length; i++) {
                for (int j = i + 1; j < redBallProbability.length; j++) {
                    for (int k = j + 1; k < redBallProbability.length; k++) {
                        for (int l = k + 1; l < redBallProbability.length; l++) {
                            for (int m = l + 1; m < redBallProbability.length; m++) {
                                for (int n = m + 1; n < redBallProbability.length; n++) {
                                    double probability = redBallProbability[i] * redBallProbability[j] * redBallProbability[k] * redBallProbability[l] * redBallProbability[m] * redBallProbability[n] * blueBallProbability[chosenBlueBall];
                                    if (probability > maxProbability) {
                                        maxProbability = probability;
                                        chosenRedBall[0] = i + 1;
                                        chosenRedBall[1] = j + 1;
                                        chosenRedBall[2] = k + 1;
                                        chosenRedBall[3] = l + 1;
                                        chosenRedBall[4] = m + 1;
                                        chosenRedBall[5] = n + 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // 输出概率最高的一组红球号码和蓝球号码
            DecimalFormat df = new DecimalFormat("#.#####");
            System.out.println("概率最高的红球号码组合为：" + chosenRedBall[0] + " " + chosenRedBall[1] + " " + chosenRedBall[2] + " " + chosenRedBall[3] + " " + chosenRedBall[4] + " " + chosenRedBall[5]);
            System.out.println("概率最高的蓝球号码为：" + chosenBlueBall);
            System.out.println("中奖率为：" + df.format(maxProbability * 100) + "%");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放连接
            httpGet.releaseConnection();
        }
    }
}